library(tidyverse)
library(tidylog)
library(ggthemes)
library(cowplot)

## get tweets time series cosine similarities

cos_simsdft <- readRDS(file = "data/analysis/cos_sims_tweets.rds")
colnames(cos_simsdft) <- c("'emergency'", "'crisis'", "'emergencies'", 
                           "'crises'", "'and'", "'the'", "timevec")

g <- cos_simsdft %>%
  arrange(timevec) %>%
  rename(yearwk = timevec) %>%
  pivot_longer(!yearwk, names_to = "term", values_to = "cos_sim") %>%
  filter(term != "'crisis'" & term != "'crises'" & term != "'emergencies'" & term != "'and'" & term!= "'the'") %>% # remove plurals and placebo words and crisis
  ggplot() +
  geom_point(aes(yearwk, cos_sim, group = term, color = term), alpha = .5) +
  geom_smooth(aes(yearwk, cos_sim, group = term, color = term),
              method = "loess",
              se = F, size = 3) +
  theme_tufte(base_family = "Helvetica") +
  labs(x = "Year-week", y = "Cosine similarity, climate : emergency", color = "Term", 
       title = "Tweets") +
  scale_color_manual(values = c("#79b321", "#578018", "#456613", "#344d0e")) +
  scale_x_date(limits = as.Date(c("2018-01-01","2020-01-01"))) +
  scale_y_continuous(breaks = seq(-.1, .1, .4)) +
  ylim(c(-.1, .5)) +
  theme(plot.title = element_text(size=25, face = "bold"),
        axis.text.x = element_text(size=15, angle = 45, hjust = 1),
        axis.text.y = element_text(size=20),
        axis.title.x = element_text(size=20),
        axis.title.y = element_text(size=20),
        legend.text=element_text(size=20),
        legend.title = element_text(size = 20),
        legend.position = "none", legend.direction = "horizontal",
        panel.border = element_rect(colour = "black", fill=NA, size=1),
        strip.text = element_text(size=20, family = "mono"),
        plot.background = element_rect(fill = "white", colour = NA),
        panel.grid.major = element_line(size = 0.1, linetype = "solid"),
        panel.grid.minor = element_line(size = 0.1, linetype = "solid")) +
  facet_wrap( ~ term, nrow = 2)

## get speeches time series cosine similarities

cos_simsdfs <- readRDS(file = "data/analysis/cos_sims_speeches.rds")
colnames(cos_simsdfs) <- c("'emergency'", "'crisis'", "'emergencies'", 
                           "'crises'", "'and'", "'the'", "timevec")

g1 <- cos_simsdfs %>%
  arrange(timevec) %>%
  rename(yearwk = timevec) %>%
  pivot_longer(!yearwk, names_to = "term", values_to = "cos_sim") %>%
  filter(term != "'crisis'" & term != "'crises'" & term != "'emergencies'" & term != "'and'" & term!= "'the'") %>% # remove plurals and placebo words and crisis
  ggplot() +
  geom_point(aes(yearwk, cos_sim, group = term, color = term), alpha = .5) +
  geom_smooth(aes(yearwk, cos_sim, group = term, color = term),
              method = "loess",
              se = F, size = 3) +
  theme_tufte(base_family = "Helvetica") +
  labs(x = "Year-week", y = "", color = "Term",
       title = "Speeches") +
  scale_color_manual(values = c("#79b321", "#578018", "#456613", "#344d0e")) +
  scale_x_date(limits = as.Date(c("2018-01-01","2019-12-01"))) +
  scale_y_continuous(breaks = seq(-.1, .1, .4)) +
  ylim(c(-.1, .5)) +
  theme(plot.title = element_text(size=25, face = "bold"),
        axis.text.x = element_text(size=15, angle = 45, hjust = 1),
        axis.text.y = element_text(size=20),
        axis.title.x = element_text(size=20),
        axis.title.y = element_text(size=20),
        legend.text=element_text(size=20),
        legend.title = element_text(size = 20),
        legend.position = "none", legend.direction = "horizontal",
        panel.border = element_rect(colour = "black", fill=NA, size=1),
        strip.text = element_text(size=20, family = "mono"),
        plot.background = element_rect(fill = "white", colour = NA),
        panel.grid.major = element_line(size = 0.1, linetype = "solid"),
        panel.grid.minor = element_line(size = 0.1, linetype = "solid")) +
  facet_wrap( ~ term, nrow = 2)

plot_grid(g, g1, ncol = 2, labels = "AUTO", label_size = 20)

ggsave("plots/fig2.png", width=300, height = 200, dpi=300, units="mm")
       